#include"ListWidget.h" 

#include<QDir>
#include<QDragEnterEvent>
#include<QDropEvent>
#include<QUrl>
#include<QFileInfo>
#include<QMimeData>

#include"Song.h"
#include"SongItem.h"
#include"ListWidgetWorker.h"
#include"MetaDataEditor.h"

ListWidget::ListWidget(QWidget *parent):QListWidget(parent){
	setDragDropMode(QAbstractItemView::DropOnly);
	setSelectionMode(QAbstractItemView::ExtendedSelection);
	m_worker = 0;
	MetaDataEditor::addCallbackOnChange(filesEdited, this);
}

ListWidget::~ListWidget(){
	
}
	
void ListWidget::dragEnterEvent ( QDragEnterEvent * event ){
	/*for(int i = 0; i < event->mimeData()->formats().size(); i++){
		printf("Mimedata format: %s\n", event->mimeData()->formats()[i].toUtf8().data());
	}*/
	if(event->mimeData()->hasUrls())
		event->accept();
}

void ListWidget::dragMoveEvent(QDragMoveEvent*){
	
}

void ListWidget::dropEvent ( QDropEvent * event ){
	if(event->mimeData()->hasUrls()){
		event->accept();
		QList<QUrl> urls = event->mimeData()->urls();
		/*printf("Urls:\n");
		for(int i = 0; i< urls.size(); i++)
			printf("\t%s\n", urls[i].toString().toUtf8().data());*/
		if(m_worker && !m_worker->isFinished()){
// 			printf("Adding urls to worker.\n");
			m_worker->addUrls(urls);
		}else{
			if(m_worker)
				delete m_worker;
			m_worker = new ListWidgetWorker;
			connect(m_worker, SIGNAL(finished()), this, SLOT(slotWorkerFinished()));
			connect(m_worker, SIGNAL(addSong(Song*)), this, SLOT(slotAddSong(Song*)));
			m_worker->addUrls(urls);
			m_worker->start();
// 			printf("ListWidget:: worker started.\n");
		}
	}
}

void ListWidget::addMedia(const QStringList &media){
	QList<QUrl> urls;
	for(int i = 0; i < media.size(); i++)
		urls.push_back(QUrl(media[i]));
	if(m_worker && !m_worker->isFinished()){
// 			printf("Adding urls to worker.\n");
		m_worker->addUrls(urls);
	}else{
		if(m_worker)
			delete m_worker;
		m_worker = new ListWidgetWorker;
		connect(m_worker, SIGNAL(finished()), this, SLOT(slotWorkerFinished()));
		connect(m_worker, SIGNAL(addSong(Song*)), this, SLOT(slotAddSong(Song*)));
		m_worker->addUrls(urls);
		m_worker->start();
	}
}

void ListWidget::slotAddSong(Song* song){
	emit addSong(song, -1);
}

void ListWidget::slotWorkerFinished(){
	if(m_worker){
		m_worker->disconnect();
		delete m_worker;
		m_worker = 0;
	}
}

void ListWidget::mousePressEvent(QMouseEvent *event){
	QListWidget::mousePressEvent(event);
	if(event->button() == Qt::RightButton && itemAt(event->pos())){
		event->accept();
		QMenu *menu = new QMenu(this);
		QAction *action = menu->addAction("Edit metadata");
		connect(action, SIGNAL(triggered()), this, SLOT(slotEditMetadata()));
		
		action = menu->addAction("Remove from playlist");
		connect(action, SIGNAL(triggered()), this, SLOT(slotRemoveFromPlaylist()));
		menu->exec(event->globalPos());
	}
}

void ListWidget::slotEditMetadata(){
	QList<QListWidgetItem*> selectedSongs = selectedItems();
	QList<QString> songPaths;
	for(int i = 0; i < selectedSongs.size(); i++){//TODO: This line looks hairy...
		songPaths.push_back( ((SongItem*)selectedSongs[i])->song()->path() );
	}
	emit requestedEditMetadata(songPaths);
}

void ListWidget::slotRemoveFromPlaylist(){
	QList<QListWidgetItem*> selectedSongs = selectedItems();
	QList<short int> indices;
	for(int i = 0; i < selectedSongs.size(); i++)
		indices.push_back( row(selectedSongs[i]) );
	emit removeSongs(indices);
}

void ListWidget::filesEdited(void *obj, QList<QString> filepaths){
	ListWidget *widget = (ListWidget*)obj;
	widget->filesEdited(filepaths);
}

void ListWidget::filesEdited(QList<QString> filepaths){
	for(int i = 0; i < count(); i++){
		SongItem *it = (SongItem*) item(i);
		if(!it)continue;//Really shouldn't happen, but still
		for(int j = 0; j < filepaths.size(); j++){
			if(it->song() && it->song()->path() == filepaths[j]){
				it->reloadMetaData();
				break;//No need to test this item against more paths now anymore.
			}
		}
	}
}
